package com.weixin;

import com.github.sd4324530.fastweixin.api.config.ApiConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一控制accessToken
 */
public class WeixinConfigUtil {

    private static final Map<String,ApiConfig> API_CONFIG_MAP = new HashMap<>();

    /**
     * 获取fastWExin的ApiConfig
     * @param appid
     * @param secret
     * @return
     */
    public static ApiConfig getApiConfig(String appid,String secret){
        return getApiConfig(appid,secret,false);
    }

    public static ApiConfig getApiConfig(String appid,String secret,boolean jsApi){
        ApiConfig apiConfig = API_CONFIG_MAP.get(appid);
        if (apiConfig == null){
            apiConfig = new ApiConfig(appid, secret,jsApi);
            API_CONFIG_MAP.put(appid,apiConfig);
        }else if (jsApi && !apiConfig.isEnableJsApi()){
            apiConfig = new ApiConfig(appid, secret,jsApi);
            API_CONFIG_MAP.put(appid,apiConfig);
        }
        return apiConfig;
    }

    public static ApiConfig get(String appid){
        return API_CONFIG_MAP.get(appid);
    }
}
